package com.xyzwps.webfluxjava;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.CacheControl;
import org.springframework.web.reactive.config.*;
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;

import org.springframework.web.reactive.function.server.support.ServerResponseResultHandler;

import java.util.concurrent.TimeUnit;

@SpringBootApplication
@MapperScan(basePackages="com.xyzwps.webfluxjava.dao.mapper")
@EnableWebFlux
public class App implements WebFluxConfigurer {

	@SuppressWarnings("all")
	@Autowired
	private ThymeleafReactiveViewResolver thymeleafReactiveViewResolver;

	/**
	 * NOTE:
	 * <p>
	 * {@link ServerResponseResultHandler} 中需要注入一些 ViewResolver 来解析视图，
	 * 可是 {@link ThymeleafReactiveViewResolver} 又不能被自动注入，于是就手动注入，
	 * 然后等配置文件 {@link DelegatingWebFluxConfiguration} 把这里注入的 thymeleafReactiveViewResolver
	 * 注入到 {@link ServerResponseResultHandler} 中了。
	 * <p>
	 * 还不够自动啊。
	 */
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.viewResolver(thymeleafReactiveViewResolver);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**")
				.addResourceLocations("/public", "classpath:/static/")
				.setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS));
	}


	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
